package com.wukaige.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wukaige.order.entity.Goods;
import com.wukaige.order.entity.Order;
import com.wukaige.order.entity.Ordergoods;
import com.wukaige.order.feign.GoodsFeign;
import com.wukaige.order.mapper.OrderMapper;
import com.wukaige.order.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wukaige.order.service.IOrdergoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wukaige
 * @since 2021-05-05
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    GoodsFeign goodsFeign;

    @Autowired
    IOrdergoodsService ordergoodsService;

    @Override
    public IPage getorderlist(Page page, Order order, Integer uid) {

        return baseMapper.getorderlist(page, order, uid);
    }

    @Override
    public List<Order> getListByStatus(Integer status) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", 1); //1：未支付
        return this.list(queryWrapper);
    }

    @Override
    public boolean cancelOrder(String Oid) {
        Order order = getById(Oid);
        QueryWrapper<Ordergoods> wrapper = new QueryWrapper();
        wrapper.eq("oid",Oid);
        List<Ordergoods> list = ordergoodsService.list(wrapper);
        for (Ordergoods ordergoods : list) {
            System.out.println("中间表list:"+ordergoods);
            Goods getgoodsbygid = goodsFeign.getgoodsbygid(ordergoods.getGid());
            System.out.println("根据订单号查询到的商品:"+getgoodsbygid);
            getgoodsbygid.setGoodsum(getgoodsbygid.getGoodsum()+getgoodsbygid.getInvent());
            Integer goodsum = getgoodsbygid.getGoodsum();
            getgoodsbygid.setInvent(0);
            Integer invent = getgoodsbygid.getInvent();
            //回滚
            goodsFeign.updatebygid(getgoodsbygid.getGid(),goodsum,invent);
        }
        order.setStatus(3); //2:已支付；3：取消订单
        return saveOrUpdate(order);
    }

    @Override
    public boolean updateByOid(String Oid) {
        Order byId = getById(Oid);
        byId.setStatus(2);
        return saveOrUpdate(byId);
    }

    @Override
    public boolean removeByOid(String Oid) {
        Order byId = getById(Oid);
        boolean b = removeById(byId);
        return b;
    }
}
